'use strict';

const BaseController = require('./base');

class CategoryController extends BaseController {
  constructor(ctx) {
    super(ctx, 'category');
  }

  // 获取分类树形结构
  async tree() {
    const { ctx } = this;
    const tree = await ctx.service.category.getTree();
    ctx.success(tree);
  }

  // 重写getPageQuery方法，支持按名称搜索
  getPageQuery(query) {
    const { name } = query;
    const where = {};
    if (name) {
      where.name = { [this.app.Sequelize.Op.like]: `%${name}%` };
    }
    return where;
  }
}

module.exports = CategoryController;
